package com.top10.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.org.apache.regexp.internal.RE;
import com.top10.bean.JwtUtil;
import com.top10.bean.PageBean;
import com.top10.bean.ResponseBean;
import com.top10.entity.Rebate;
import com.top10.entity.Rebatelink;
import com.top10.service.impl.GoodsServiceImpl;
import com.top10.service.impl.RebateLinkServiceImpl;
import com.top10.service.impl.ShopServiceImpl;
import com.top10.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/manager/rebate")
public class RebateController {

     @Autowired
	 RebateLinkServiceImpl rebatelinkService;
     @Autowired
	UserServiceImpl userService;
     @Autowired
	ShopServiceImpl shopService;
     @Autowired
	GoodsServiceImpl goodsService;

	@PostMapping("/selectlinkone")
	public ResponseBean selectLinkOne(HttpServletRequest request)throws Exception{
		String userUuid = JwtUtil.getUuidByToken(request);
		List rebatelinkList1=new ArrayList<>();
		List<Rebatelink> rebatelinkList=rebatelinkService.getLinkOne();
		if(rebatelinkList.size()==0){
			return ResponseBean.createSuccess("没有注册链接推广");
		}
		for(Rebatelink rebatelink:rebatelinkList){
			String reline=rebatelink.getRebate_link()+"?uid="+userUuid+"&rid="+rebatelink.getUuid()+"&type="+rebatelink.getRebate_shoptype();
			rebatelinkList1.add(reline);
		}

		return ResponseBean.createSuccess().addData("rebatelinkList",rebatelinkList1);
	}

	@PostMapping("/selectlinktwo")
	public ResponseBean selectLinkTwo(HttpServletRequest request)throws Exception{
		String userUuid = JwtUtil.getUuidByToken(request);
		List rebatelinkList1=new ArrayList<>();
		List<Rebatelink> rebatelinkList=rebatelinkService.getLinkTwo();
		if(rebatelinkList.size()==0){
			return ResponseBean.createSuccess("没有商品推广链接");
		}
		for(Rebatelink rebatelink:rebatelinkList){
			String reline=rebatelink.getRebate_link()+"?uid="+userUuid+"&rid="+rebatelink.getUuid()+"&type="+rebatelink.getRebate_shoptype();
			rebatelinkList1.add(reline);
		}


		return ResponseBean.createSuccess().addData("rebatelinkList",rebatelinkList1);
	}
	@PostMapping("/getpersonal")
	public ResponseBean getPersonal(PageBean pageBean, HttpServletRequest request)throws Exception{

		/*String uuid="f0e225b41d4a4715b942f9e1897a1c8b";*/
		Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
		String uuid = JwtUtil.getUuidByToken(request);
		List<Rebate> rebateList1=new ArrayList<>();
		List<Rebate> rebateList =rebatelinkService.getPersonal(uuid);
		if(rebateList != null) {
			for (Rebate rebate : rebateList) {
				String head_sculpture = userService.getHeadSculpture(rebate.getUser_uuid());
				rebate.setHead_sculpture(head_sculpture);
				rebateList1.add(rebate);
			}

			for (Rebate rebate : rebateList) {
				List<Rebate> rebate1 = rebatelinkService.getRebate(rebate.getRuser_uuid());
				if(rebate1 !=null){
					for(Rebate rebate2: rebate1){
						String head_sculpture = userService.getHeadSculpture(rebate2.getUser_uuid());
						rebate2.setHead_sculpture(head_sculpture);
						rebateList1.add(rebate2);
					}
				}
			}
			for (Rebate rebate : rebateList) {
				List<Rebate> rebate2 = rebatelinkService.getRebateOne(rebate.getPuser_uuid());
				if(rebate2 !=null){
					for (Rebate rebate1:rebate2){
						String head_sculpture = userService.getHeadSculpture(rebate1.getUser_uuid());
						rebate1.setHead_sculpture(head_sculpture);
						rebateList1.add(rebate1);
					}
				}


			}
		}

			List<Rebate> rebateList2=rebatelinkService.getPersonalOne(uuid);
			if(rebateList2 !=null){
				for(Rebate rebate:rebateList2){
					String head_sculpture=userService.getHeadSculpture(rebate.getUser_uuid());
					rebate.setHead_sculpture(head_sculpture);
					rebateList1.add(rebate);
				}
				for(Rebate rebate:rebateList2){
					List<Rebate> rebate1 = rebatelinkService.getRebate(rebate.getRuser_uuid());
					for(Rebate rebate2 :rebate1){
						String head_sculpture=userService.getHeadSculpture(rebate.getUser_uuid());
						rebate2.setHead_sculpture(head_sculpture);
						rebateList1.add(rebate2);
					}
				}
			}

			List<Rebate> rebateList3=rebatelinkService.getPersonalTwo(uuid);
			if(rebateList3 !=null){
				for(Rebate rebate:rebateList3){
					String head_sculpture=userService.getHeadSculpture(rebate.getUser_uuid());
					rebate.setHead_sculpture(head_sculpture);
					rebateList1.add(rebate);
				}
			}
		pageBean= new PageBean(page.getPageNum(),page.getPageSize(), (int) page.getTotal());
		return ResponseBean.createSuccess().addData("rebateList",rebateList1).addData("pageBean",pageBean);
	}


	@PostMapping("/getbusiness")
	public ResponseBean getBusiness(PageBean pageBean, HttpServletRequest request)throws Exception {

		/*String uuid="f0e225b41d4a4715b942f9e1897a1c8b";*/
		Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
		String uuid = JwtUtil.getUuidByToken(request);
		List<Rebate> rebateList1 = new ArrayList<>();
		List<Rebate> rebateList = rebatelinkService.getBusiness(uuid);
		if (rebateList != null) {
			for (Rebate rebate : rebateList) {
				String head_sculpture = userService.getHeadSculpture(rebate.getUser_uuid());
				rebate.setHead_sculpture(head_sculpture);
				rebateList1.add(rebate);
			}

			for (Rebate rebate : rebateList) {
				List<Rebate> rebate1 = rebatelinkService.getRebate(rebate.getRuser_uuid());
				if (rebate1 != null) {
					for (Rebate rebate2 : rebate1) {
						String head_sculpture = userService.getHeadSculpture(rebate2.getUser_uuid());
						rebate2.setHead_sculpture(head_sculpture);
						rebateList1.add(rebate2);
					}
				}
			}
			for (Rebate rebate : rebateList) {
				List<Rebate> rebate2 = rebatelinkService.getRebateOne(rebate.getPuser_uuid());
				if (rebate2 != null) {
					for (Rebate rebate1 : rebate2) {
						String head_sculpture = userService.getHeadSculpture(rebate1.getUser_uuid());
						rebate1.setHead_sculpture(head_sculpture);
						rebateList1.add(rebate1);
					}
				}


			}
		}

		List<Rebate> rebateList2 = rebatelinkService.getPersonalOne(uuid);
		if (rebateList2 != null) {
			for (Rebate rebate : rebateList2) {
				String head_sculpture = userService.getHeadSculpture(rebate.getUser_uuid());
				rebate.setHead_sculpture(head_sculpture);
				rebateList1.add(rebate);
			}
			for (Rebate rebate : rebateList2) {
				List<Rebate> rebate1 = rebatelinkService.getRebate(rebate.getRuser_uuid());
				for (Rebate rebate2 : rebate1) {
					String head_sculpture = userService.getHeadSculpture(rebate.getUser_uuid());
					rebate2.setHead_sculpture(head_sculpture);
					rebateList1.add(rebate2);
				}
			}
		}

		List<Rebate> rebateList3 = rebatelinkService.getPersonalTwo(uuid);
		if (rebateList3 != null) {
			for (Rebate rebate : rebateList3) {
				String head_sculpture = userService.getHeadSculpture(rebate.getUser_uuid());
				rebate.setHead_sculpture(head_sculpture);
				rebateList1.add(rebate);
			}
		}
		pageBean = new PageBean(page.getPageNum(), page.getPageSize(), (int) page.getTotal());
		return ResponseBean.createSuccess().addData("rebateList", rebateList1).addData("pageBean", pageBean);
	}


	@PostMapping("/getgood")
	public ResponseBean getGood(PageBean pageBean, HttpServletRequest request)throws Exception {

		/*String uuid="f0e225b41d4a4715b942f9e1897a1c8b";*/
		Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
		String uuid = JwtUtil.getUuidByToken(request);
		List<Rebate> rebateList1 = new ArrayList<>();
		List<Rebate> rebateList = rebatelinkService.getPersonal(uuid);
		if (rebateList != null) {
			for (Rebate rebate : rebateList) {
				String show = goodsService.getShow(rebate.getRebate_productid());
				rebate.setShow(show);
				rebateList1.add(rebate);
			}

			for (Rebate rebate : rebateList) {
				List<Rebate> rebate1 = rebatelinkService.getRebate(rebate.getRuser_uuid());
				if (rebate1 != null) {
					for (Rebate rebate2 : rebate1) {
						String show = goodsService.getShow(rebate2.getRebate_productid());
						rebate2.setShow(show);
						rebateList1.add(rebate2);
					}
				}
			}
			for (Rebate rebate : rebateList) {
				List<Rebate> rebate2 = rebatelinkService.getRebateOne(rebate.getPuser_uuid());
				if (rebate2 != null) {
					for (Rebate rebate1 : rebate2) {
						String show = goodsService.getShow(rebate1.getRebate_productid());
						rebate1.setShow(show);
						rebateList1.add(rebate1);
					}
				}
			}
		}
		List<Rebate> rebateList2 = rebatelinkService.getPersonalOne(uuid);
		if (rebateList2 != null) {
			for (Rebate rebate : rebateList2) {
				String show = goodsService.getShow(rebate.getRebate_productid());
				rebate.setShow(show);
				rebateList1.add(rebate);
			}
			for (Rebate rebate : rebateList2) {
				List<Rebate> rebate1 = rebatelinkService.getRebate(rebate.getRuser_uuid());
				for (Rebate rebate2 : rebate1) {
					String show = goodsService.getShow(rebate2.getRebate_productid());
					rebate2.setShow(show);
					rebateList1.add(rebate2);
				}
			}
		}

		List<Rebate> rebateList3 = rebatelinkService.getPersonalTwo(uuid);
		if (rebateList3 != null) {
			for (Rebate rebate : rebateList3) {
				String show = goodsService.getShow(rebate.getRebate_productid());
				rebate.setShow(show);
				rebateList1.add(rebate);
			}
		}
		pageBean = new PageBean(page.getPageNum(), page.getPageSize(), (int) page.getTotal());
		return ResponseBean.createSuccess().addData("rebateList", rebateList1).addData("pageBean", pageBean);
	}
}
